package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 地图
 *
 * @author chart
 */
@RestController
@RequestMapping("/map/chart")
public class MapChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;




    //杭州市xx信息区域分布示意图
    @PostMapping("/regionalDistribution/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            if (ObjectUtils.isEmpty(getdata.get(0).get(1))) {
                return AjaxResult.error("请填充最小值数据");
            }
            if (ObjectUtils.isEmpty(getdata.get(1).get(1))) {
                return AjaxResult.error("请填充最大值数据");
            }

            String minV = getdata.get(0).get(1);
            String maxV = getdata.get(1).get(1);
            String infoV = getdata.get(2).get(1);


            List<JSONObject> maps = new ArrayList<>();
            for (int i = 4; i < getdata.size(); i++) {
                List<String> strings = getdata.get(i);
                if (!CollectionUtils.isEmpty(strings)) {
                    JSONObject map = new JSONObject();
                    for (int i1 = 0; i1 < strings.size(); i1++) {
                        String s = strings.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i1 == 0) {
                                map.put("name", s);
                            } else {
                                map.put("value", s);
                            }
                        }
                    }
                    maps.add(map);
                }
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            jsonObject1.put("name", infoV);
            jsonObject1.put("data", maps);

            String minMaxAxis = JSON.toJSONString(jsonObject.get("visualMap"));
            JSONObject jsonObjectMinMAx = JSON.parseObject(minMaxAxis);
            jsonObjectMinMAx.put("min", Double.parseDouble(minV));
            jsonObjectMinMAx.put("max", Double.parseDouble(maxV));
            jsonObject.put("visualMap", jsonObjectMinMAx);


            jsonObject.put("series", jsonObject1);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

}
